package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dto.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    /*
    * TODO todo 敏感词查询\分页、关键字收索
    *  */
    @Override
    public ResponseResult queueList(SensitiveDTO sensitiveDTO) {
        //todo 参数检查
        if (sensitiveDTO==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //todo 分页检测
        sensitiveDTO.checkParam();
        //TODO 分页参数检测
        IPage<AdSensitive> pageRe = new Page<>(sensitiveDTO.getPage(), sensitiveDTO.getSize());
        //todo 根据敏感词模糊查询
        LambdaQueryWrapper<AdSensitive> lambdaQuery = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(sensitiveDTO.getName())) {
            lambdaQuery.like(AdSensitive::getSensitives, sensitiveDTO.getName());
        }

        //分页参数
        IPage<AdSensitive> pageResult = this.page(pageRe, lambdaQuery);
        //TODO 封装结果
        PageResponseResult responseResult =
                new PageResponseResult(sensitiveDTO.getPage(), sensitiveDTO.getSize(), pageResult.getTotal());

        responseResult.setData(pageResult.getRecords());

        return responseResult;
    }

    /*
     * todo 敏感词新增
     *  */
    @Override
    public ResponseResult saveAdSensitive(AdSensitive adSensitive) {
        //TODO 参数校验
        String sensitives = adSensitive.getSensitives();
        if (StringUtils.isBlank(sensitives)||sensitives.length()>20){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空，长度不能大于20");
        }
        //todo 查询敏感词是否重复
        LambdaQueryWrapper<AdSensitive> lambdaQuery = Wrappers.lambdaQuery();
        LambdaQueryWrapper<AdSensitive> eq = lambdaQuery.eq(AdSensitive::getSensitives, sensitives);
        int count = this.count(eq);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在");
        }
        //todo 保存敏感词
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        return ResponseResult.okResult();
    }

    /*
     * TODO 敏感词修改
     *  */
    @Override
    public ResponseResult updateAdSensitive(AdSensitive adSensitive) {
        //TODO  参数校验
        //todo 检查id是否为空
        if (adSensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id 不能为空");
        }
        //todo 根据id检查修改以前的数据
        AdSensitive byId = getById(adSensitive.getId());
        if (byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"未发现对应敏感词的信息");
        }
        String sensitives = adSensitive.getSensitives();

        if (StringUtils.isNotBlank(sensitives)&&!sensitives.equals(byId.getSensitives())){
            LambdaQueryWrapper<AdSensitive> lambdaQuery = Wrappers.lambdaQuery();
            LambdaQueryWrapper<AdSensitive> eq = lambdaQuery.eq(AdSensitive::getSensitives, sensitives);
            int count = this.count(eq);
            if (count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该敏感词已存在，请换一个");
            }
        }
        this.updateById(adSensitive);
        return ResponseResult.okResult();
    }

    /*
     * TODO 敏感词删除
     *  */
    @Override
    public ResponseResult deleteById(Integer id) {

        //todo 检查参数
        //todo 检查id是否为空
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"id 不能为空");
        }
        //todo 根据id检查数据
        AdSensitive adSensitive = getById(id);
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"数据不存在");
        }
        this.deleteById(id);
        return ResponseResult.okResult();
    }

    /*
    * TODO 查询敏感词内容list
    *  */
    @Autowired
    private AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult queueAllSensitives() {
        List<String> sensitivesList = this.adSensitiveMapper.findAllSensitives();
        return ResponseResult.okResult(sensitivesList);
    }

}
